<?php
/**
 * Created by PhpStorm.
 * User: yangt
 * Date: 2020/2/19
 * Time: 21:01
 */

namespace app\survey\controller;

class Project extends SurveyHome {

	public function index($code)
	{
		$map  = ['status' => 1,'code'=>$code];
		$info = model('survey')->where($map)->find();
		if(empty($info)) {
			$this->error('参数错误或当前调查已被下线');
		}
		$this->assign('info', $info);

		//处理问卷页
		$page_id  = input('page_id', 0);
		$page_map = ['survey_id' => $info['id']];
		if(empty($page_id)) {
			$page = model('surveyPage')->where($page_map)->order('sort desc,id asc')->find();
		} else {
			$page = model('surveyPage')->where($page_map)->find($page_id);
		}

		if(empty($page)) {
			$this->error('当前调查没有关联的问卷页');
		}

		$question_map  = ['page_id' => $page['id'], 'status' => 1];
		$question_list = model('surveyQuestion')->where($question_map)->select();
		if( ! empty($question_list)) {
			//处理问题选项
			foreach($question_list as &$question_item) {
				$item_map                   = ['question_id' => $question_item['id'], 'status' => 1];
				$item_list                  = model('surveyItem')->where($item_map)->order('id asc')->select();
				$question_item['item_list'] = $item_list;
			}
		}

		$this->assign('page', $page);
		$this->assign('question_list', $question_list);

		$this->themeLayout('single')->themeSet()->themeInit();
		return $this->fetch('project/'.$info['theme']);
	}
}